﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using WMS.BLL;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WMS.Model;
using System.Data;
using Newtonsoft.Json.Converters;
using System.Xml;
using System.Runtime.Serialization.Json;
using System.Text;
/// <summary>
///StockIn 的摘要说明
/// </summary>
public class StockIn : HandlerBase
{
    StockInManage manage = new StockInManage();
    public void EntTMCode()
    {
        string tm = _request["tm"];
        List<StockInModel> mdLs = manage.GetProductInfoByEntCode(tm);

        string jsonStr = string.Empty;
        if (mdLs == null || mdLs.Count < 1)
        {
            jsonStr = "{\"error\":\"无条码信息\"}";
        }
        else
        {
            var jsonObj = mdLs.Select(m => new { m.Tm, m.ProductCode, m.Color, m.Mh, m.Qty, m.Price, m.Total, user = _user.UserName, m.Date });
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd:hh:mm:ss" };// { DateTimeFormat = "yyyy-MM-dd:" };
            jsonStr = JsonConvert.SerializeObject(jsonObj.Single(), dtConverter);
        }
        _response.Write(jsonStr);

    }

    /// <summary>
    /// 入库操作
    /// </summary>
    public void StockAdd()
    {
        //{'storagedata':[{'Tm':'jjj','PrdCode':'yy','Color':'白色','Size':'24','SaleCode':'26','Count':'20','Money':'12.55','PoId':'xxx001','User':'aa','Dt':'01-01-2012'}]}"
        string ls = _request["stockLs"];
        string whs = _request["whs"];
        JsonTransfer jt = new JsonTransfer();
        if (!string.IsNullOrEmpty(ls))
        {
            try
            {
                string xml = string.Empty;
                XmlDocument xmlDoc = new XmlDocument();
                XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(ls), XmlDictionaryReaderQuotas.Max);
                xmlDoc.Load(reader);
                xml = xmlDoc.InnerXml;
                _user.WHSSWHS = whs;
                string bilCode = manage.StockInList(xml, _user);
                if (!string.IsNullOrEmpty(bilCode))
                {
                    DataTable posList = manage.GetILIPOSInfoByBil(bilCode); 
                    jt.Add("list", posList);
                }
                else
                {
                    jt.SetError("入库失败");
                }   
            }
            catch (Exception e)
            {
                jt.SetError(e.Message);
            }
            _response.Write(jt);
        }
    }

    /// <summary>
    /// 获取入库单明细信息
    /// </summary>
    /// <param name="bil"></param>
    /// <returns></returns>
    public void GetStockBilDetail()
    {
        string bil = _request["bil"];
        JsonTransfer jt = new JsonTransfer();
        DataTable dt = manage.GetPosBilDetailInfo(bil);
        jt.Add("list", dt);
        _response.Write(jt);
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    public void GetUserDefuatInfo()
    {
        JsonTransfer js = new JsonTransfer();
        js.Add("user", _user);
        _response.Write(js);
    }

    public void IsTmExist()
    {
        string tm = _request["tm"];
        JsonTransfer js = new JsonTransfer();
        if(manage.ExistTmInITHPOS(tm))
            js.Add("exist", "Y", false);
        else
            js.Add("exist", "N", false);
        _response.Write(js);
    }

    public void GetWHSInfo()
    {
        JsonTransfer js = new JsonTransfer();
        string supId = _user.CII;
        DataTable dt =  manage.GetWHSInfoBysupId(supId);
        js.Add("list", dt);
        _response.Write(js);
    }
}